#include "loginThread.h"
LoginThread::LoginThread(QString tmpuserName,QString tmppasswd,QString tmp,bool R,bool S)
{
    userName=tmpuserName;
    passwd=tmppasswd;
    workDir=tmp;
    r=R;
    s=S;
}
void LoginThread::run()
{
    ctl_sock = tcp_connect(SRV_IP,SRV_CTL_PORT);
    if(ctl_sock < 0)
    {
        sleep(5);
        ctl_sock = tcp_connect(SRV_IP,SRV_CTL_PORT);
        if(ctl_sock<0)
        {
            sleep(5);
            ctl_sock = tcp_connect(SRV_IP,SRV_CTL_PORT);
            if(ctl_sock<0)
            {
                sleep(5);
                ctl_sock = tcp_connect(SRV_IP,SRV_CTL_PORT);
                if(ctl_sock<0)
                {
                    perror("login cant reach server");

                    emit this->loginFailed(1);
                    return;
                }
            }
        }
    }
        //打包
        ctl_pkg_t info;
        info.type = LOGIN;
        strncpy(info.str.id.usr,userName.toStdString().c_str(),USR_LEN);
        strncpy(info.str.id.pwd,passwd.toStdString().c_str(),PWD_LEN);

        //发送给服务器
        if( writen(ctl_sock,&info,sizeof(ctl_pkg_t)) < 0 )
        {     
            emit this->loginFailed(2);
            ::close(ctl_sock);
            return;
        }

        //读取服务器的应答
        int8_t reply;
        if( readn(ctl_sock,&reply,sizeof(reply)) < 0 )
        {

            emit this->loginFailed(3);
            ::close(ctl_sock);
            return;
        }

        //reply<0说明登录失败
        if (reply != 0)
        {

            emit this->loginFailed(4);;
            ::close(ctl_sock);
            return;
        }


        //将用户信息存到passwd文件，下次用这个用户自动登录
        QString passFilePath=workDir+"/.passwd";
        QFile passFile(passFilePath);
        if(passFile.open(QFile::WriteOnly))
        {
            QTextStream in(&passFile);
            if(r)
                in<<1<<"\n";
            else
                in<<0<<"\n";
            in<<userName<<"\n";
            in<<passwd<<"\n";
        }
        passFile.close();

        //看本地有没有用户文件，如果没有，新建该用户文件，如果有该用户，改写
        //QString userFile=workDir+"/"+userName;
        QString userDir=workDir+"/"+userName;
        QDir testUserDir;
        if(!testUserDir.exists(userDir))
        {
            testUserDir.mkpath(userDir);
            userList<<userName<<"\n";
            QString userListFile=workDir+"/users";
            QFile File(userListFile);
            if(File.open(QFile::WriteOnly|QFile::Append))
            {
                QTextStream in(&File);
                in<<userName<<"\n";
            }
            File.close();

            QString userHomeDir;
            userHomeDir=QDir::homePath()+"/"+"GeeBox/"+userName;
            testUserDir.mkpath(userHomeDir);
            QString userFile=userDir+"/"+userName;
            QFile file(userFile);
            if(file.open(QFile::WriteOnly))
            {
                QTextStream in(&file);
                if(s)
                {
                    in<<1<<"\n";
                    in<<passwd<<"\n";
                }
                else
                {
                    in<<0<<"\n";
                    in<<passwd<<"\n";
                }
                in<<userHomeDir<<"\n";
            }
            file.close();

            //为新用户创建版本文件
            QString versionFilePath=userDir+"/.version";
            QFile versionFile(versionFilePath);
            if(versionFile.open(QFile::WriteOnly))
            {
                QTextStream in(&versionFile);
                in<<int(0)<<"\n";
            }
            versionFile.close();
        }
        else//已经有了这个用户修改用户的信息
        {
            QString userFile=userDir+"/"+userName;
            QFile file(userFile);
            int tmp;
            QString tmpPasswd;
            QString tmpUserDir;
            if(file.open(QFile::ReadOnly))
            {
                QTextStream in(&file);
                in>>tmp;
                in>>tmpPasswd;
                in>>tmpUserDir;
            }
            file.close();
            if(file.open(QFile::WriteOnly))
            {
                QTextStream out(&file);
                if(s)
                {
                    out<<1<<"\n";
                }
                else
                {
                    out<<0<<"\n";
                }
                out<<tmpPasswd<<"\n";
                out<<tmpUserDir<<"\n";
            }
            file.close();
        }

        //登录完成，开始进行初始化
        emit hasloged(ctl_sock,userName);
}
